/**
 * Title: SubtaskDeptScoreServiceImpl.java
 *
 * @author WuJin
 * @date 2020年6月17日
 * @version 1.0
 */
package com.pm.service.task.impl;

import com.pm.entity.po.task.SubtaskDeptTask;
import com.pm.entity.vo.dept.DeptScore;
import com.pm.entity.vo.dept.DeptScoreQuarterVo;
import com.pm.entity.vo.dept.DeptTaskNumVo;
import com.pm.entity.vo.task.TaskVo;
import com.pm.mapper.caveatConf.CaveatConfMapper;
import com.pm.mapper.task.SubtaskDeptTaskMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.task.SubtaskDeptTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Title: SubtaskDeptScoreServiceImpl
 *
 * @author WuJin
 * @date 2020年6月17日
 */
@Service
public class SubtsaskDeptTaskServiceImpl extends BaseServiceImpl<SubtaskDeptTask> implements SubtaskDeptTaskService {
    private SubtaskDeptTaskMapper subtaskDeptTaskMapper;
    @Autowired
    CaveatConfMapper caveatConfMapper;

    @Resource
    public void setBasemapper(SubtaskDeptTaskMapper subtaskDeptTaskMapper) {
        this.subtaskDeptTaskMapper = subtaskDeptTaskMapper;
        super.setBaseMapper(subtaskDeptTaskMapper);
    }

    /* (non-Javadoc)
     * <p>Title: addBatch</p>
     * <p>Description: </p>
     * @param scores
     * @return
     * @see com.pm.service.task.SubtaskDeptScoreService#addBatch(java.util.List)
     */
    @Override
    public int removeBatch(SubtaskDeptTask subtaskDeptTask) {
        return subtaskDeptTaskMapper.removeBatch(subtaskDeptTask);
    }

    /* (non-Javadoc)
     * <p>Title: getDeptList</p>
     * <p>Description: </p>
     * @param subtaskDeptTask
     * @return
     * @see com.pm.service.task.SubtaskDeptTaskService#getDeptList(com.pm.entity.po.task.SubtaskDeptTask)
     */
    @Override
    public List<DeptScore> getDeptList(SubtaskDeptTask subtaskDeptTask) {
        /*// 重点工作的配合单位数量
        Integer num = subtaskDeptTaskMapper.getNum(subtaskDeptTask);
        // 查亮灯配置
        CaveatConf caveatConf = new CaveatConf();
        caveatConf = caveatConfMapper.getOne(num);
        if (caveatConf != null) {
            // 前几名
            Integer after = caveatConf.getAfter();
            // 后几名
            Integer before = caveatConf.getBefore();

            List<DeptScore> deptScores = subtaskDeptTaskMapper.getDeptList(subtaskDeptTask);
            if (deptScores.size() > 1) {
                for (int i = 0; i < deptScores.size(); i++) {
                    // 前几名
                    if (i < after) {
                        deptScores.get(i).setLamp(1);
                    }
                    // 后几名
                    if (i + 1 > (deptScores.size() - before)) {
                        deptScores.get(i).setLamp(0);
                    }
                }
            }
            return deptScores;
        }*/
        List<DeptScore> deptScores = subtaskDeptTaskMapper.getDeptList(subtaskDeptTask);
        return deptScores;
    }

    @Override
    public List<DeptScoreQuarterVo> getDeptQuarter(DeptScoreQuarterVo deptScoreQuarterVo) {
        return subtaskDeptTaskMapper.getDeptQuarter(deptScoreQuarterVo);
    }

    @Override
    public List<Map<String, String>> getUpQuarterDept(DeptScoreQuarterVo deptScoreQuarterVo) {
        return subtaskDeptTaskMapper.getUpQuarterDept(deptScoreQuarterVo);
    }

    @Override
    public List<Map<String, Object>> getDeptTaskNum(DeptTaskNumVo deptTaskNumVo) {
        return subtaskDeptTaskMapper.getDeptTaskNum(deptTaskNumVo);
    }

    @Override
    public List<Map<String, Object>> getDeptTypeTaskNum(TaskVo taskVo) {
        return subtaskDeptTaskMapper.getDeptTypeTaskNum(taskVo);
    }

    @Override
    public List<Map<String, Object>> getDeptByTaskId(TaskVo taskVo) {
        return subtaskDeptTaskMapper.getDeptByTaskId(taskVo);
    }

    @Override
    public Boolean getDeptQuarterExamine(DeptScoreQuarterVo deptScoreQuarterVo) {
        // 根据季度查询未填写成绩的有多少个
        Integer countNum = subtaskDeptTaskMapper.getDeptQuarterExamine(deptScoreQuarterVo);
        if (countNum > 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<Integer> getDeptQuarterIds(DeptScoreQuarterVo deptScoreQuarterVo) {
        return subtaskDeptTaskMapper.getDeptQuarterIds(deptScoreQuarterVo);
    }

    @Override
    public List<DeptScore> getDeptScoreList(SubtaskDeptTask subtaskDeptTask) {
        List<DeptScore> deptScoreList = subtaskDeptTaskMapper.getDeptListAll(subtaskDeptTask);
        // 双层for循环，外小内大效率比较高
        // 季度循环
        for (int i = 1; i <= 4; i++) {
            subtaskDeptTask.setQuarter(i);
            List<Map<String, Object>> quarterScoreList = subtaskDeptTaskMapper.getDeptScoreListByTaskId(subtaskDeptTask);
            for (DeptScore deptScore : deptScoreList) {
                if (i == 1) {
                    for (Map<String, Object> map : quarterScoreList) {
                        if (map.get("departmentName").equals(deptScore.getDepartmentName())) {
                            deptScore.setCaveat1((Integer) map.get("caveat"));
                            if (map.get("score") != null) {
                                deptScore.setScore1(Double.valueOf(map.get("score").toString()));
                            } else {
                                deptScore.setScore1(null);
                            }
                            deptScore.setRank1((Integer) map.get("rank"));
                            deptScore.setStatus1((Integer) map.get("status"));
                            deptScore.setRankDescription1((String) map.get("rankDescription"));
                        }
                    }
                }
                if (i == 2) {
                    for (Map<String, Object> map : quarterScoreList) {
                        if (map.get("departmentName").equals(deptScore.getDepartmentName())) {
                            deptScore.setCaveat2((Integer) map.get("caveat"));
                            if (map.get("score") != null) {
                                deptScore.setScore2(Double.valueOf(map.get("score").toString()));
                            } else {
                                deptScore.setScore2(null);
                            }
                            deptScore.setRank2((Integer) map.get("rank"));
                            deptScore.setStatus2((Integer) map.get("status"));
                            deptScore.setRankDescription2((String) map.get("rankDescription"));
                        }
                    }
                }
                if (i == 3) {
                    for (Map<String, Object> map : quarterScoreList) {
                        if (map.get("departmentName").equals(deptScore.getDepartmentName())) {
                            deptScore.setCaveat3((Integer) map.get("caveat"));
                            if (map.get("score") != null) {
                                deptScore.setScore3(Double.valueOf(map.get("score").toString()));
                            } else {
                                deptScore.setScore3(null);
                            }
                            deptScore.setRank3((Integer) map.get("rank"));
                            deptScore.setStatus3((Integer) map.get("status"));
                            deptScore.setRankDescription3((String) map.get("rankDescription"));
                        }
                    }
                }
                if (i == 4) {
                    for (Map<String, Object> map : quarterScoreList) {
                        if (map.get("departmentName").equals(deptScore.getDepartmentName())) {
                            deptScore.setCaveat4((Integer) map.get("caveat"));
                            if (map.get("score") != null) {
                                deptScore.setScore4(Double.valueOf(map.get("score").toString()));
                            } else {
                                deptScore.setScore4(null);
                            }
                            deptScore.setRank4((Integer) map.get("rank"));
                            deptScore.setStatus4((Integer) map.get("status"));
                            deptScore.setRankDescription4((String) map.get("rankDescription"));
                        }
                    }
                }
            }
        }
        return deptScoreList;
    }

}
